package com.zfl.service.impl;

import com.zfl.dto.query.SeatQuery;
import com.zfl.entity.Seat;
import com.zfl.mapper.SeatMapper;
import com.zfl.service.SeatService;
import com.zfl.vo.result.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SeatServiceImpl implements SeatService {
    private final SeatMapper seatMapper;

    @Override
    public PageResult<Seat> listOrdinarySeats(SeatQuery query) {
        List<Seat> seats = seatMapper.selectOrdinarySeats(query);
        return buildPageResult(seats, query);
    }

    @Override
    public PageResult<Seat> listLectureSeats(SeatQuery query) {
        List<Seat> seats = seatMapper.selectLectureSeats(query);
        return buildPageResult(seats, query);
    }

    @Override
    public PageResult<Seat> listAllSeats(SeatQuery query) {
        List<Seat> seats = seatMapper.selectAllSeats(query);
        return buildPageResult(seats, query);
    }

    // 封装分页结果
    // 手动内存分页逻辑
    private PageResult<Seat> buildPageResult(List<Seat> fullList, SeatQuery query) {
        PageResult<Seat> result = new PageResult<>();
        result.setTotal((long) fullList.size()); // 总条数
        result.setPages((int) Math.ceil((double) fullList.size() / query.getPageSize())); // 总页数
        result.setPageNum(query.getPageNum());
        result.setPageSize(query.getPageSize());
        result.setList(fullList); // 内存截取当前页
        return result;
    }
}